package cn.middle.application.remote;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.middle.common.result.ApiResponse;

import cn.middle.application.dto.DoorUserDto;
import cn.middle.application.entity.DoorUser;
import cn.middle.application.entity.Park;
import cn.middle.application.service.DoorUserService;
import cn.middle.application.service.ParkService;
import cn.middle.remote.serialization.TypeGenericity;
import cn.middle.remote.user.UserClient;
import cn.middle.remote.util.GsonUtil;

@Component
public class DoorRemoteApi {
	
	@Autowired
	private UserClient client;
	
	@Autowired
	private ParkService parkService;
	
//	@Autowired
//	private EdgeServiceService edgeService;
	
	@Autowired
	private DoorUserService doorUserService;
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	

	public void sendAddUser(DoorUser user) {
		String msgId = UUID.randomUUID().toString();
		String interfaceName = "addUser";
		int remoteClientId = parkService.byParkCode(user.getParkId()).getRemoteId();
		
		DoorUserDto dto = new DoorUserDto();
		dto.setId(user.getId());
		dto.setName(user.getName());
		dto.setPhone(user.getPhone());
		dto.setSex(user.getSex());
		dto.setServerCode(user.getServiceId());
		try {
			ApiResponse<Boolean> apiResponse = this.client.call(remoteClientId, interfaceName, msgId, dto).get(new TypeGenericity<ApiResponse<Boolean>>() {}, 30000);
			logger.info("{} --> result  {}",interfaceName,GsonUtil.toJson(apiResponse));
		} catch (Throwable e) {
			logger.error(e.getMessage(),e);
		}
		
	}
	
	
	public DoorUserDto sendGetUserById(Long id) {
		String msgId = UUID.randomUUID().toString();
		String interfaceName = "findById";
		int remoteClientId = this.remoteClientId(id);
		try {
			ApiResponse<DoorUserDto> apiResponse = this.client.call(remoteClientId, interfaceName, msgId, id).get(new TypeGenericity<ApiResponse<DoorUserDto>>() {}, 30000);
			logger.info("{} --> result  {}",interfaceName,GsonUtil.toJson(apiResponse));
			return apiResponse.getData();
		} catch (Throwable e) {
			logger.error(e.getMessage(),e);
		}

		return null;
	}
	
	public boolean sendDeleteUser(Long id) {
		String msgId = UUID.randomUUID().toString();
		String interfaceName = "deleteUser";
		int remoteClientId = this.remoteClientId(id);
		try {
			ApiResponse<Boolean> apiResponse = this.client.call(remoteClientId, interfaceName, msgId, id).get(new TypeGenericity<ApiResponse<Boolean>>() {}, 30000);
			logger.info("{} --> result  {}",interfaceName,GsonUtil.toJson(apiResponse));
	        return apiResponse.isSuccess();
		} catch (Throwable e) {
			logger.error(e.getMessage(),e);
		}
		return false;
	}
	
	
	private int remoteClientId(Long id) {
		DoorUser user = doorUserService.getById(id);
		if(user != null) {
			Park park = parkService.byParkCode(user.getParkId());
			return park.getRemoteId();
		}
		throw new NullPointerException("the remoteId is null");
	}
	
	public boolean subscribe(int eventKey) {
		try {
			String msgId = UUID.randomUUID().toString();
			this.client.subscribe(msgId, eventKey);
			return true;
		}catch(Exception e) {
			
		}
		return false;
	}
}
